/**
 * Copyright(c) 2014 Xunton Solutions
 *
 * History:
 *   15-5-22 下午12:37 Created by dwzhang
 */
package com.jade.bss.api.controller;

import java.util.HashMap;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletResponse;

import com.jade.bss.base.customer.Customer;
import com.jade.bss.beacon.Beacon;
import com.jade.bss.beacon.BeaconEvent;
import com.jade.bss.beacon.BeaconManager;
import com.jade.bss.beacon.location.Location;
import com.jade.bss.beacon.uuid.Uuid;
import com.jade.bss.beacon.uuid.UuidManager;
import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.Restrictions;
import com.jade.framework.base.util.PagingList;
import com.jade.framework.base.vfile.VFileConstants;
import com.jade.framework.base.vfile.VFileUtils;
import com.jade.framework.base.util.ResponseUtils;
import com.jade.modules.software.Software;
import com.jade.modules.software.SoftwareUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * BeaconController.
 *
 * @author <a href="mailto:dwzhang@xunton.com">dwzhang</a> 
 * @version 1.0 15-5-22 下午12:37
 */
@Controller
@RequestMapping (value = "/beacon")
public class BeaconController
{
    private BeaconManager beaconManager;
    private UuidManager uuidManager;

    @Inject
    public void setUuidManager(@Named ("jade_beacon_uuidManager") UuidManager uuidManager)
    {
        this.uuidManager = uuidManager;
    }

    @Inject
    public void setBeaconManager(@Named ("bss_beaconManager") BeaconManager beaconManager)
    {
        this.beaconManager = beaconManager;
    }

    @RequestMapping (value = "/checkFirmwareVersion.json")
    public void checkVersion(@RequestParam (value = "hardwareVersion", required = false) String hardwareVersion,
                             @RequestParam (value = "firmwareVersion", required = false) String firmwareVersion,
                             HttpServletResponse response)
            throws Exception
    {
        String type = "beacon_firmware";
        Software software = SoftwareUtils.getNewestSoftware(type);
        HashMap result = new HashMap();
        result.put("update", false);
        if (software != null) {
            if (firmwareVersion.compareToIgnoreCase(software.getVersion()) < 0) {
                result.put("update", true);
            }
            result.put("version", software.getVersion());
            result.put("remark", software.getRemark());
            result.put("url", VFileUtils.getVFileURI(VFileConstants.DATA_TYPE_SOFTWARE, software.getFilePath()));
        }
        ResponseUtils.writeSuccessResult(response, result);
    }

    @RequestMapping (value = "/query.json")
    @RequiresAuthentication
    public void query(@RequestParam (value = "offset", defaultValue = "0") int offset,
                      @RequestParam (value = "pageSize", defaultValue = "20") int length, HttpServletResponse response)
            throws Exception
    {
        Subject user = SecurityUtils.getSubject();
        Customer customer = (Customer) user.getPrincipal();
        Criteria criteria = new Criteria(offset, length);
        criteria.add(Restrictions.eq("ownerId", String.valueOf(customer.getId())));
        criteria.add(Restrictions.eq("ownerType", customer.getOwnerType()));
        PagingList<Beacon> beacons = beaconManager.queryBeacon(criteria);
        ResponseUtils.writeSuccessPagingResult(response, offset, length, beacons, beacons.getTotalCount());

    }

    @RequestMapping (value = "/update.json")
    @RequiresAuthentication
    public void update(@RequestBody Beacon beacon, HttpServletResponse response)
            throws Exception
    {
        beaconManager.reportBeacon(beacon);
        ResponseUtils.writeSuccessResult(response);
    }

    @RequestMapping (value = "/listValidUUID.json")
    @RequiresAuthentication
    public void listValidUUID(@RequestParam (value = "offset", defaultValue = "0") int offset,
                              @RequestParam (value = "pageSize", defaultValue = "20") int length,
                              HttpServletResponse response)
            throws Exception
    {
        Criteria criteria = new Criteria(offset, length);
        criteria.add(Restrictions.eq("status", Uuid.STATUS_AVAILABLE));
        List<Uuid> uuids = uuidManager.list(criteria);
        ResponseUtils.writeSuccessResult(response, uuids);
    }

    @RequestMapping (value = "/report.json")
    public void report(@RequestBody BeaconEvent event,HttpServletResponse response) throws Exception{
        Location location = beaconManager.handleEvent(event);
//        HashMap<String,Item> result = new HashMap<String, Item>();
//        if (location!=null&&location.getItems().size()>0){
//            result.put("enter",location.getFirstEnterItem());
//            result.put("stay",location.getFirstStayItem());
//            result.put("leave",location.getFirstLeaveItem());
//            result.put("nearby",location.getFirstNearbyItem());
//        }
        ResponseUtils.writeSuccessResult(response,location);
    }
}
